<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;

/**
 * Created by zhangyh.
 * User: zhangyh
 * Date: 2018/11/3
 * Time: 17:40
 */
class BaseModel extends Model
{

    private $pageSize = 15;
    public $request;

    public function __construct()
    {
        $this->request = app('request');
    }

    public function getListWithPage($where = [], $orderBy = '', $order = '',$with=[])
    {
        $page         = $this->request->input('page', '1');
        $page         = intval($page);
        $skip         = ($page - 1) * $this->perPage;
        $num = $this->count($where);
        $pageNum = ceil($num/$this->pageSize);
        $pageInfo = ['pageNum'=>$pageNum,'page'=>$page,'pageSize'=>$this->pageSize];
        $query        = $this->where($where);
        if ($orderBy && $order) {
            $query->orderBy($orderBy, $order);
        }
        $query = $query->skip($skip)->take($this->pageSize)->get();
        if ($with){
            foreach ($with as $item) {
                $query->load($item);
            }
        }
        $data = $query->toArray();
        return ['pageInfo' => $pageInfo, 'data' => $data];
    }


    public function count($where = [])
    {
        return $this->where($where)->count();
    }

    /**
     * 预加载关联关系
     * @param  array  $withAll [对应的关联关系数据]
     * @return [type]          [description]
     */
    public function withAll($withAll=[])
    {
        if (!empty($withAll)) {
           $this->with = $withAll;
        }
        return $this;
    }

    /**
     * 分页获取活动列表数据
     * @param  array   $where    [数据条件]
     * @param  string  $orderBy  [排序字段]
     * @param  string  $order    [排序类型]
     * @param  boolean $is_page  [是否分页]
     * @param  integer $pageSize [每页显示的数量]
     * @return [obj]            [模型列表实例]
     */
    public function getAllList($where=[],$orderBy='created_at',$order='desc',$is_page=true,$pageSize=15)
    {
        $select = $this;
        if ($this->with) {
            $select = $select->with($this->with);
        }
        if ($where) {
            $select = $select->where($where);
        }
        $select = $select->orderBy($orderBy,$order);

        if ($is_page) {
            $list = $select->paginate($pageSize);
        }else {
            $list = $select->all();
        }
        return $list;
    }

    /**
     * 根据id获取相应单条记录数据
     * @param  [integer] $id [应对ID]
     * @return [obj]     [对应模型实例]
     */
    public function getRow($id)
    {
        $select = $this;
        if ($this->with) {
            $select = $select->with($this->with);
        }
        $data = $select->find($id);

        return $data;
    }

    /**
     * 根据条件获取相应单条记录数据
     * @param  [array] $where [条件数组]
     * @return [obj]     [对应模型实例]
     */
    public function getRowByCondition($where)
    {
        $select = $this;
        if ($this->with) {
            $select = $select->with($this->with);
        }
        $data = $select->where($where)->first();

        return $data;
    }

    /**
     * 添加数据
     * @param [type] $data [要添加的数据]
     * @return obj [模型实例]
     */
    public function add($data)
    {
        return $this->create($data);
    }

    /**
     * 更新单条记录
     * @param  [integer] $id   [对应id]
     * @param  [array] $data [需要更新的数据]
     * @return [bool]       [true]
     */
    public function saveRow($id,$data)
    {
        $select = $this->find($id);
        $select->fill($data);
        $select->save();
        return true;
    }

    /**
     * 批量更新数据
     * @param  [array] $where [条件数组]
     * @param  [array] $data  [需要更新的数据]
     * @return [integer]        [返回更新的数量]
     */
    public function updateBatch($where,$data)
    {
        return $this->where($where)->update($data);
    }

    /**
     * 根据id删除单条数据
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function deleteRow($id)
    {
        return $this->destroy($id);
    }
    /**
     * 批量删除数据
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function deleteBatch($where)
    {
        return $this->where($where)->delete();
    }

}